package com.aircms.controller;

import com.aircms.common.Common;
import com.aircms.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 后台系统基类
 */
public class BaseAdminController implements HandlerInterceptor {
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;


    private HttpServletRequest request;
    private String token;


    /**
     * 获取登录用户的名称
     * @return
     */
    protected String getUserName(){
        init();
        return JwtUtil.parserJwt(token,"name");
    }


    /**
     * 获取登录用户的id
     * @return
     */
    protected int getUserId(){
        init();
        return Integer.parseInt(JwtUtil.parserJwt(token,"id"));
    }


    /**
     * 获取登录用户的角色id
     * @return
     */
    protected String getUserRoleId(){
        init();
        return JwtUtil.parserJwt(token,"role_ids");
    }

    protected Map getAllRoutes(){
        Map map =  handlerMapping.getHandlerMethods();

        return map;
//        try {
//            Map map =  handlerMapping.getHandlerMethods();
////            Iterator<?> iterator = map.entrySet().iterator();
////            while(iterator.hasNext()){
////                Map.Entry entry = (Map.Entry) iterator.next();
////                System.out.println(entry.getKey() +"\n" + entry.getValue());
////            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }

    /**
     * 获取分页，第几页
     * 默认第一页
     * @return
     */
    protected  int getPageNum(){
        init();
        String pageNumS=request.getParameter("pageNum");
        if(!StringUtils.isEmpty(pageNumS)&&Common.isInteger(pageNumS)){
            return Integer.parseInt(pageNumS);
        }
        return 1;
    }


    /**
     * 设置分页大小
     * 默认20
     * @return
     */
    protected int getPageSize(){
        init();
        String pageSizeS=request.getParameter("pageSize");
        if(!StringUtils.isEmpty(pageSizeS)&&Common.isInteger(pageSizeS)){
            if(Integer.parseInt(pageSizeS)>1000){
                return 1000;
            }
            return Integer.parseInt(pageSizeS);
        }
        return 20;
    }


    /**
     * 记录错误日志
     * @param object
     * @param errString
     */
    protected void errorLog(Object object, Exception errString){
        Logger logger=LoggerFactory.getLogger((Class<?>) object);
        logger.error(String.valueOf(errString));
    }


    private void init(){
        ServletRequestAttributes requestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        request= requestAttributes.getRequest();
        token=request.getHeader("Authorization");
    }



}
